package com.first_javaee.webcontoiller;

import com.first_javaee.service.UserInfoSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class WebStudentController { //每个功能做成一个请求
    @Autowired
    private UserInfoSerivce userInfoSerivce;    //自动装配
    //系统自动UserInfoSerivce userInfoSerivce=new UserInfoSerivce();对象并且管理起来见64行;
   @RequestMapping("show_login")
    public String showLogin(){
       return "login";
       //在@Controller注解下，返回templates目录下同名网页
       //在@RestController注解下，返回login字符串
   }
   @RequestMapping("check_login")
    public String checkLogin(HttpServletRequest request,Model model){    //凡事要接收参数的都要加上request变量
       String user_name=request.getParameter("user_name");
       String password=request.getParameter("pwd");
//       System.out.println(user_name+"---"+password);
       if (user_name.equals("thj")&&password.equals("123456")){
           //添加数据到前端
           model.addAttribute("user_name",user_name);
           model.addAttribute("s_no",229350501);
           model.addAttribute("sex","男");
           return "main";//不是重定向，直接映射main
       }else{
           return "redirect:show_login";//重定向，对应的请求名(直接执行show_login)
       }
   }
    @RequestMapping("register_account")
    public String registerAccount(){
       return "register";
   }
   @RequestMapping("return_login")
    public String returnLogin(){
       return "login";
   }
   @RequestMapping("register")
    public String register(HttpServletRequest request) {
       StringBuilder sb = new StringBuilder();
       String user_name = request.getParameter("user_name");
       String password = request.getParameter("pwd");
       String qrpassword = request.getParameter("qrpwd");
       String result = sb.append("username:").append(user_name).append("password:").append(password).toString();
       if (password.equals(qrpassword)) {
           //往数据库插入一条数据
           System.out.println(result);
           userInfoSerivce.insertData(user_name,password);

       }
           return "redirect:show_login";

   }
   @RequestMapping("check_login_mybatis")
    public String checkLoginMybatis(HttpServletRequest request,Model model){
       String  user_name=request.getParameter("userName");
       String  user_pwd=request.getParameter("userPwd");
       System.out.println("webController:user_Name:"+user_name+"user_pwd:"+user_pwd);
//       UserInfoSerivce userInfoSerivce=new UserInfoSerivce();
       boolean flag= userInfoSerivce.checkLogin(user_name,user_pwd);
       if (flag){
           model.addAttribute("user_name",user_name);
           return "main";
       }else {
           return "redirect:show_login";    //重定向对应的是请求名
       }
   }
}
// 请求-->网页-->请求-->网页